// list.js
var app = getApp()
var lastClickTime = 0
var minClickTime = app.globalData.minClickTime
var encryptUtils = require('../../../utils/HHEncryptUtils.js')
var modelUtils = require('../../../utils/HHModelUtils.js')
var baseImgPath = app.globalData.imgBasepath
var pageSize = app.globalData.pageSize
var default_img = baseImgPath + app.globalData.default_img
var pageIndex = 1
var loading = false;

Page({
   /**
    * 页面的初始数据
    */
   data: {
      loading: true,
      complate: false,
      noData: false,
      loadMore: false,
      noDataMsg: "暂无数据",
      loaingImg: baseImgPath + app.globalData.loading,
      noDataImg: baseImgPath + app.globalData.no_data,

   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
      this.getPageData()
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function () {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
      this.getPageData()
   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function () {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function () {

   },
   getPageData: function () {

      var para = {}
      para["page"] = encryptUtils.base64Encode(pageIndex);
      para["page_size"] = encryptUtils.base64Encode(pageSize);
      // 在此添加请求参数额外


      var str = JSON.stringify(para)
      console.log("para str==" + str)
      var urlStr = encodeURIComponent(str)
      console.log("para==" + urlStr)
      loading = true;
      var that = this
      wx.request({
         url: app.globalData.ip + "goodscollection",
         method: 'POST',
         header:
         {
            'From': "WeChat"
         },
         data:
         {
            'para': urlStr
         },
         success: function (res) {
            var code = res.data.code
            console.log("res==" + JSON.stringify(res.data))

            if (code == 100) {
               var templeList = modelUtils.getList(res.data.result)
               if (pageIndex == 1) {
                  that.setData(
                     {
                        loading: false,
                        complate: true,
                        noData: false,
                        list: templeList,
                        loadMore: templeList.length == pageSize
                     }
                  )
               } else {
                  var list = this.data.list
                  list = list.concat(templeList)
                  that.setData(
                     {
                        list: list,
                        loadMore: templeList.length == pageSize
                     }
                  )
               }

            } else {
               if (pageIndex == 1) {

                  that.setData(
                     {
                        loading: false,
                        complate: true,
                        noData: true,
                        noDataMsg: "暂无数据"
                     }
                  )
               } else {
                  var message = res.data.msg
                  if (code == 101) {
                     that.setData(
                        {
                           loadMore: false
                        }
                     )
                  }
                  wx.showToast({
                     title: message,
                  })
               }
            }

         },
         fail: function () {
            wx.showToast({
               title: '网络异常',
            })
            if (pageIndex == 1) {
               that.setData(
                  {
                     loading: false,
                     complate: true,
                     noData: true,
                     noDataMsg: "加载失败，请刷新重试"
                  }
               )
            } else {
               var message = res.data.msg
               wx.showToast({
                  title: message,
               })
            }
         },
         complete: function () {
            wx.stopPullDownRefresh();
            loading = false;

         }


      })

   },

   getMoreData: function () {

      var loadMore = this.data.loadMore

      if (loadMore && !loading) {
         pageIndex++
         this.getPageData()
      }

   },
})